@<template>
    
  <div id="home">
    <back-top v-show="isShow" @click.native="backTop"></back-top>
    <nav-bar class="home-nav"><div slot="mid_bar">购物街</div></nav-bar>
    <tab-control ref="tabControl2" @tochange="toChose" v-show='controlShow' id='topControl'/>
    <scroll
      ref="scroll"
      @showBack="showBack"
      :probe-type="probeType"
      :pull-up-load="true"
      @isEnd="isEnd"
    >
      <home-swiper :banners="banners" @swiperLoad='swiperLoad' />
      <home-recommends :recommends="recommends" />
      <feature-view />
      <tab-control   @tochange="toChose" ref="tabControl" />
      <goods-list :goodsList="goods[`${type}`].list"></goods-list>
    </scroll>
  </div>
</template>

<script>
//主页轮播图组件
import HomeSwiper from "./childComps/HomeSwiper.vue";

//轮播图下面的4张图片的组件
import HomeRecommends from "./childComps/HomeRecommends.vue";

//一张图片的组件
import FeatureView from "./childComps/FeatureView.vue";

//商品列表组件
import GoodsList from "components/content/goods/GoodsList.vue";

//顶部导航栏组件
import NavBar from "components/common/navbar/NavBar.vue";


//流行.新款.精选 可点击组件
import TabControl from "components/content/tabControl/TabControl";

//页面滚动组件
import Scroll from "components/common/scroll/Scroll.vue";

//返回顶部组件
import BackTop from "components/content/backTop/BackTop.vue";

//导入获取主页参数封装好的方法
import { getHomeMultidata, getHomeGoods } from "network/home";

//导入防抖函数
import {debounce} from '@/common/utils'

export default {
  components: {
    HomeSwiper,
    HomeRecommends,
    FeatureView,
    NavBar,
    TabControl,
    GoodsList,
    Scroll,
    BackTop,
  },

  data() {
    return {
      banners: [], //轮播图广告
      recommends: [], //首页4张圆形的图片数据

      //存放首页推荐,流行,新款,精选 数据
      goods: {
        pop: { page: 0, list: [] },
        new: { page: 0, list: [] },
        sell: { page: 0, list: [] },
      },
      type: "pop", //首次请求类型 默认流行
      isShow: false, //返回最顶端按钮,默认不显示

      // 默认开启屏幕滚动侦听事件,好判断何时显示返回顶端按钮
      probeType: 2,
      scrollInstance: null,
      tabObj2: {},
      tabControlTop: 0,
      controlShow: false,
      leavTop: 0,
      homeImageLoad: null,
    };
  },

  //生命周期钩子, 创建元素后调用
  created() {
    //接收服务器数据,
    //调用方法
    this.getHomeMultidata();
    this.fistGet("pop");
    this.fistGet("new");
    this.fistGet("sell");
  },
  mounted() {

   

    //是scroll实例, 用来获取组件高度
    this.tabObj2 = this.$refs.tabControl.$el
     //重新刷新组件高度方法
    const ref = debounce(this.$refs.scroll.refresh);
    //监听事件总线,图片加载完成事件
    this.$bus.$on("imgLoad", () => {
      ref();
      
    });
  },
  activated(){
    this.$refs.scroll.scroll.scrollTo(0, this.leavTop, 0);
    this.$refs.scroll.refresh();
  },
  deactivated(){
    //取消监听网页加载事件
    this.leavTop = this.$refs.scroll.scroll.y;
    
  },
  methods: {
    //初始化化访问,只访问一次
    fistGet(fistType) {
      let page = 1;
      this.goods[this.type].page += 1;
      getHomeGoods(fistType, page).then((res) => {
        this.goods[fistType].list.push(...res.data.list);
      });
    },

    //接收首页goodslist数据并保存
    getHomeGood() {
      this.goods[this.type].page += 1;
      let type = this.type;
      let page = this.goods[this.type].page;

      getHomeGoods(type, page).then((res) => {
        this.goods[type].list.push(...res.data.list);

        this.scrollInstance?.finishPullUp();
      });
    },

    //接收轮播图数据并保存
    getHomeMultidata() {
      getHomeMultidata().then((res) => {
        // this.multidata = res,
        this.banners = res.data.banner.list;
        this.recommends = res.data.recommend.list;
      });
    },

    //修改默认请求类型 'pop'
    toChose(type, index) {
      this.$refs.tabControl2.currentInde = index;
      this.$refs.tabControl.currentInde = index;
      this.type = type;
    },

    //判读是否显示 返回顶端按钮
    showBack(position) {
      if (Math.abs(position.y) > 2000) {
        this.isShow = true;
      } else {
        this.isShow = false;
      }

      //
      if(Math.abs(position.y)>= this.tabControlTop) {
        this.controlShow = true
      }else {
        this.controlShow = false
      }

    },

    //用户点击返回顶端按钮事件
    backTop() {
      this.isShow = false;
      this.$refs.scroll.scroll.scrollTo(0, 0, 500);
    },

    //到达底部开始加载数据
    isEnd(scrollInstance) {
      this.scrollInstance = scrollInstance;
      setTimeout(() => {
        this.getHomeGood(); //请求数据
      }, 3000);
    },
    //图片加载完成后获取组件到顶部的距离
    swiperLoad(){
      this.tabControlTop = this.tabObj2.offsetTop
    }
    
  },
  computed: {
    getTop(){
      return this.tabObj2.offsetTop
    }
  }
};
</script>

<style>
#home {
  width: 100%;
  /* padding-top: 44px; */
}
.home-nav {
  /* position: sticky; */
  /* position: fixed; */
/* z-index: 1; */
  /* left: 0;
  top: 0;
   */
  background-color: var(--color-tint);
  color: #fff;
  font-size: 18px;
  box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
  line-height: 44px;
}

#home {
  height: 100vh;
}
.wiapper {
  position: relative;
  height: calc(100% - 93px);
  overflow: hidden;
  
}
.back-top {
  position: absolute;
  z-index: 2;
  bottom: 50px;
  right: 10px;
}
#topControl {
  position: fixed;
  width: 100%;
  z-index: 3;
  background-color: #fff;
}

ul li img {
  width: 100%;
}
</style>